import Vue from 'vue'
import Router from 'vue-router'
import Login from '../view/Login'
import '../assets/css/global.css'
import HelloPage from '../components/HelloPage'
import BossHome from '../view/BossHome'
import CashierHome from '../view/CashierHome'

import StaffItem from '../components/user/StaffItem'
import SupplierItem from '../components/user/SupplierItem'
import Cash from '../components/cash/Cash'
import PersonalMGT from '../components/personal/PersonalMGT'
import Sales from '../components/sales/Sales'
import RefundCash from '../components/cash/RefundCash'
import Stock from '../components/stock/Stock'
Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', name: 'Login', component: Login },
    {
      path: '/bosshome',
      name: 'BossHome',
      component: BossHome,
      children:[
        { path: '/staffitem0', name: 'StaffItem0', component: StaffItem },
        { path: '/supplieritem0', name: 'SupplierItem0', component: SupplierItem },
        { path: '/', name: 'HelloPage0', component: HelloPage },
        { path: '/cash0', name: 'Cash0', component: Cash },
        { path: '/personalmgt0', name: 'PersonalMGT0', component: PersonalMGT },
        { path: '/sales0', name: 'Sales0', component: Sales },
        { path: '/refundcash0', name: 'RefundCash0', component: RefundCash },
        { path: '/stock0', name: 'Stock0', component: Stock }
        ]
    },
    {
      path: '/cashierhome',
      name: 'CashierHome',
      component: CashierHome,
      children:[
        { path: '/', name: 'HelloPage1', component: HelloPage },
        { path: '/cash1', name: 'Cash1', component: Cash },
        { path: '/refundcash1', name: 'RefundCash1', component: RefundCash },
        { path: '/personalmgt1', name: 'PersonalMGT1', component: PersonalMGT },
        { path: '/stock1', name: 'Stock1', component: Stock }
        ]
    },


  ]
})

//挂载导航守卫
router.beforeEach((to, from, next) => {
  //to 将要访问的路径
  //from 从哪个路径跳转来
  //next是一个函数，表示放行  next()放行，next('/login/)强制跳转

  if (to.path == '/login') return next()
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
